﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using Yuki.Sockets;

namespace YukiFX5017_Login
{
    class Program
    {
        public static string DatabasePath;
        static void Main()
        {
            DatabasePath = @"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\Testing\Database";
            ConquerSocket authSocket = new ConquerSocket();
            authSocket.Port = 9958;
            authSocket.ClientBufferSize = 100;
            authSocket.Backlog = 1;
            authSocket.OnClientConnect = new SocketEvent<YukiSocketClient, object>(Program.AuthConnect);
            authSocket.OnClientReceive = new SocketEvent<YukiSocketClient, byte[]>(Program.AuthReceive);
            authSocket.Enable();
            Console.Title = "YukiFX 5017 LoginServer";
            Console.Clear();
            Console.WriteLine("Online! :)");
            while (true)
                Console.ReadLine();
        }
        public static void AuthConnect(YukiSocketClient Sender, object param)
        {
            Sender.Wrapper = new AuthClient(Sender);
        }
        public static void AuthReceive(YukiSocketClient Sender, byte[] param)
        {
            AuthClient Client = (AuthClient)Sender.Wrapper;
            if (Sender.RecvSize == 52)
            {
                byte[] Recv = param;
                if (BitConverter.ToUInt16(Recv, 2) == 0x41B)
                {
                    byte i = 0;
                    Client.Username = Encoding.ASCII.GetString(Recv, 4, 16).Trim(new char[] { (char)0x0000 });
                    Client.Password = "";
                    while (i < 16)
                    {
                        Client.Password += Recv[i + 16].ToString("X2");
                        i = (byte)(i + 1);
                    }
                    if (IsValidAccount(Client, ref Client.UID))
                    {
                        Sender.Send(AuthResponse("192.168.43.110", Client.UID, Client.UID));
                    }
                    else
                    {
                        Sender.Disconnect();
                    }
                }
            }
        }
        public static unsafe byte[] AuthResponse(string ip, uint key1, uint key2)
        {
            ushort PacketType = 0x41f;
            byte[] Packet = new byte[32];
            fixed (byte* p = Packet)
            {
                *((ushort*)p) = (ushort)Packet.Length;
                *((ushort*)(p + 2)) = PacketType;
                *((uint*)(p + 4)) = key2;
                *((uint*)(p + 8)) = key1;
                for (int i = 0; i < ip.Length; i++)
                {
                    *(p + 12 + i) = Convert.ToByte(ip[i]);
                }
                *((short*)(p + 28)) = 5816;
            }
            return Packet;
        }
        public static bool IsValidAccount(AuthClient Client,ref uint UID)
        {
            IniFile rdr = new IniFile(DatabasePath + @"\Accounts\" + Client.Username + ".ini");
            if (rdr.ReadString("Account", "Username", "null") == Client.Username)
            {
                string Password = rdr.ReadString("Account", "Password", "", 40);
                if (Password == "")
                {
                    Password = Client.Password;
                    Client.UID = Convert.ToUInt32(File.ReadAllText(@"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\Testing\Database\UIDs.txt")) + 1;
                    rdr.Write("Account", "Password", Password);
                    rdr.Write("Account", "UID", Client.UID);
                    File.WriteAllText(@"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\Testing\Database\UIDs.txt", Client.UID.ToString());
                }
                if (Password == Client.Password)
                {
                    UID = rdr.ReadUInt32("Account", "UID", 0);
                }
            }
            else
            {
                rdr.Write("Account", "Username", Client.Username);
                Client.UID = Convert.ToUInt32(File.ReadAllText(@"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\Testing\Database\UIDs.txt")) + 1;
                File.WriteAllText(@"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\Testing\Database\UIDs.txt", Client.UID.ToString());
                rdr.Write("Account", "UID", Client.UID);
            }
            return (UID != 0);
        }
    }
}
